<?php

namespace App\Listeners\LikeCreated;

use App\Events\LikeCreated;
use App\Models\Comment;
use App\Models\Topic;
use App\Notifications\CommentLiked;
use App\Notifications\TopicLiked;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class SendNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  LikeCreated  $event
     * @return void
     */
    public function handle(LikeCreated $event)
    {
        $like = $event->like;
        $target = $like->target;
        $targetType = $like->target_type;
        $targetUser = $target->user;
        $likeUser = $like->user;
        $log = __METHOD__ . ", target_type: $targetType, targetUser: " . $targetUser->id;
        if ($likeUser->id == $targetUser->id) {
            logger(sprintf('%s, likeUid: %s = targetUid: %s, skip', $log, $likeUser->id, $targetUser->id));
            return;
        }
        if ($targetType == Comment::class) {
            if ($target->is_main_floor) {
                $targetUser->notify(new TopicLiked($target->topic, $likeUser, $like));
                $log .= ", send notification TopicLiked";
            } else {
                $targetUser->notify(new CommentLiked($target, $likeUser, $like));
                $log .= ", send notification CommentLiked";
            }
        } else {
            $log .= ", not support target type";
        }
        logger($log);
    }
}
